package com.giscloud.limp.service.impl;

import cn.hutool.core.date.DateUtil;
import com.giscloud.token.utils.TokenUserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.commons.model.Result;
import cn.hutool.core.collection.CollectionUtil;

import java.util.Date;
import java.util.List;
import com.giscloud.limp.entity.LegalProjectExample;
import com.giscloud.limp.entity.dto.LegalProjectDTO;
import com.giscloud.limp.dao.LegalProjectDao;
import com.giscloud.limp.entity.LegalProject;
import com.giscloud.limp.service.LegalProjectService;
import lombok.extern.log4j.Log4j2;


@Service("legalProjectService")
@Log4j2
public class LegalProjectServiceImpl implements LegalProjectService {
    private final  LegalProjectDao legalProjectDao;
    @Autowired
    private TokenUserUtil tokenUserUtil;
    @Autowired
    public LegalProjectServiceImpl(LegalProjectDao legalProjectDao) {
        this.legalProjectDao = legalProjectDao;
    }
    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(LegalProjectDTO legalProjectDTO) {
        log.debug("#queryPage parameter ={}", JSON.toJSONString(legalProjectDTO));
        PageHelper.startPage(legalProjectDTO.getPage(), legalProjectDTO.getRows());
        LegalProjectExample legalProjectExample = new LegalProjectExample();
        LegalProjectExample.Criteria criteria = legalProjectExample.createCriteria();
        if(StringUtils.isNotBlank(legalProjectDTO.getLegalProjectName())){
            criteria.andLegalProjectNameLike('%'+legalProjectDTO.getLegalProjectName()+'%');
        }
        if(StringUtils.isNotBlank(legalProjectDTO.getCreatedDtStart())){
            criteria.andCreatedDtGreaterThanOrEqualTo(DateUtil.parseDate(legalProjectDTO.getCreatedDtStart()));
        }
        if(StringUtils.isNotBlank(legalProjectDTO.getCreatedDtEnd())){
            criteria.andCreatedDtLessThanOrEqualTo(DateUtil.parseDateTime(legalProjectDTO.getCreatedDtEnd()+" 23:59:59"));
        }
        if(legalProjectDTO.getLegalProjectType() != null){
            criteria.andLegalProjectTypeEqualTo(legalProjectDTO.getLegalProjectType());
        }
        criteria.andStatusCdEqualTo(1);
        legalProjectExample.setOrderByClause("created_dt desc");
        List<LegalProject> legalProjects = legalProjectDao.selectByExample(legalProjectExample);
        return PageInfo.of(legalProjects);
    }


    /**
     * 信息
     */
    @Override
    public LegalProject getById(Integer legalProjectId){
        log.debug("#getById parameter ={}", legalProjectId);
        return legalProjectDao.selectByPrimaryKey(legalProjectId);
    }

    /**
     * 保存
     */
    @Override

    public Result save(LegalProject legalProject){
        log.debug("#save parameter ={}", JSON.toJSONString(legalProject));
        legalProject.setCreatedStaffId(tokenUserUtil.getUserId().intValue());
        legalProject.setStatusCd(1);
        legalProject.setCreatedDt(new Date());
        if(legalProjectDao.insertSelective(legalProject) > 0 ){
            return Result.success("保存成功");
        }else{
            return Result.error("保存失败");
        }
    }

    /**
     * 修改
     */
    @Override

    public Result updateById(LegalProject legalProject){
         log.debug("#updateById parameter ={}", JSON.toJSONString(legalProject));
         LegalProjectExample legalProjectExample = new LegalProjectExample();
         LegalProjectExample.Criteria criteria = legalProjectExample.createCriteria();
         criteria.andLegalProjectIdEqualTo(legalProject.getLegalProjectId());
         legalProject.setVersion(new Date());
         legalProject.setOperateStaffId(tokenUserUtil.getUserId().intValue());
         legalProject.setVersion(new Date());
         if(legalProjectDao.updateByExampleSelective(legalProject,legalProjectExample) > 0){
             return Result.success("修改成功");
         }else{
             return Result.error("修改失败");
         }
    }
    /**
     * 删除
     */
    @Override

    public Result removeByIds(List<Integer> legalProjectIds){
        log.debug("#removeByIds parameter ={}", JSON.toJSONString(legalProjectIds));
        LegalProjectExample legalProjectExample = new LegalProjectExample();
        LegalProjectExample.Criteria criteria = legalProjectExample.createCriteria();

            criteria.andLegalProjectIdIn(legalProjectIds);

        if(legalProjectDao.deleteByExample(legalProjectExample) > 0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

}